package com.gugee.ins.common.dal.service.brand.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.brand.InsAiBloggerBrandExtMapper;
import com.gugee.ins.common.dal.service.brand.InsAiBloggerBrandDao;
import com.gugee.ins.common.model.mysql.brand.InsAiBloggerBrand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * InsAiBloggerBrandDaoImpl
 * @author Huangxin
 */
@Slf4j
@Service
public class InsAiBloggerBrandDaoImpl extends ServiceImpl<InsAiBloggerBrandExtMapper, InsAiBloggerBrand> implements InsAiBloggerBrandDao {
    /**
     * 根据uid 删除
     * @param uid
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByUid(long uid){
        baseMapper.deleteByUid(uid);
    }

    /**
     * 批量插入
     * @param insAiBloggerBrands
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(List<InsAiBloggerBrand> insAiBloggerBrands) {
        try{
            this.saveBatch(insAiBloggerBrands,insAiBloggerBrands.size() + 1);
        }catch (Exception e){
            log.debug("batch insert ins AiBloggerBrands failed ,try insert one by one, msg : {} ",e.getMessage());
            insAiBloggerBrands.forEach(insAiBloggerBrand -> {
                try{
                    baseMapper.insert(insAiBloggerBrand);
                }catch (Exception ex){
                    log.info("insert ins AiBloggerBrands failed  ,uid: {} ,bid : {},brandName : {}, msg : {} "
                            ,insAiBloggerBrand.getUid(),insAiBloggerBrand.getBid()
                            ,insAiBloggerBrand.getBrandName(),ex.getMessage());
                }
            });
        }
    }
}
